const Koa = require('koa');
const fs = require('fs');
const path = require('path');

const filePath = path.join(path.resolve(__dirname, "./Atomics.html"))
const jsPath = path.join(path.resolve(__dirname, "./Atomics.js"))

const app = new Koa()

app.use((ctx, next) => {

    const req = ctx.req

    const url = req.url

    if (url === "/") {
        // 设置响应头
        ctx.set("Cross-Origin-Embedder-Policy", "require-corp");
        ctx.set("Cross-Origin-Opener-Policy", "same-origin");

        const file = fs.readFileSync(filePath)

        const text = file.toString("utf-8")

        ctx.type = "html"
        // Pass to next layer of middleware

        ctx.body = text
    } else if (url === "/Atomics.js") {
        const file = fs.readFileSync(jsPath)
        const text = file.toString("utf-8")
        ctx.body = text
    } else {
        console.log(url);
        ctx.body = "nothing"
    }
})

app.listen("5500", () => {
    console.log("server listening on 5500");
})
